package Offer;

/**
 * @author lakersUp
 * @Classname  isBalanced
 * @Description     判断是否为平衡二叉树
 * @Date 2022/2/27 16:21
 * @Version 1.0
 */
public class Offer55isBalanced {
    boolean flag=true;
    public boolean isBalanced(TreeNode root){
        dfs(root);
        return flag;
    }

    private int dfs(TreeNode root) {
        if(root==null){
            return 0;
        }
        int l=dfs(root.left);
        int r=dfs(root.right);
        if(Math.abs(l-r)>1){
            flag=false;
        }
        //剪枝
        if(!flag){
            //随便return一个数
            return -1;
        }
        return Math.max(l,r)+1;
    }
}
